//JavaScript代码区域
layui.use(['element', 'table', 'upload', 'layer', 'laytpl'], function () {
    var $ = layui.$,
        element = layui.element,
        table = layui.table,
        upload = layui.upload,
        layer = layui.layer,
        laytpl = layui.laytpl;
    var train_day_id = $('#train_day_id').val();
    //方法级渲染
    var field = {'id': train_day_id};
    var tableIns = table.render({
        elem: '#LAY_table',
        url: '/admin/train/timetable_day',
        method: 'POST',
        where: field,
        cols: [
            [
                {field: 'id', title: 'ID', width: 80, sort: true, fixed: false, align: 'center'},
                {field: 'thisday', title: '车次运行日期', width: 120, align: 'center'},
                {field: 'train', title: '车次', width: 120, align: 'center'},
                {field: 'station', title: '站点', width: 120, align: 'center'},
                {field: 'station_code', title: '站点三字码', minwidth: 120, align: 'center'},
                {field: 'a_time', title: '到站时间', width: 120, align: 'center'},
                {field: 'l_time', title: '发车时间', width: 120, align: 'center'},
                {field: 'state_time', title: '停车时间', width: 120, align: 'center'},
                {fixed: 'right', width: 200,toolbar: '#barTable', align: 'center'}
            ]
        ],
        page: false,
        done: function (res, curr, count) {
        }
    });
    //监听工具条
    table.on('tool(table)', function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (layEvent === 'edit') {
            var url = '/admin/train/today_timetable_edit?id=' + data.id;
            location.href = url;
        } else if (layEvent === 'forbidden') {
            var status, status_text;
            if (data.status == 0) {
                status = 1;
                status_text = '启用';
            } else {
                status = 0;
                status_text = '禁用';
            }
            var isLoad;
            $.ajax({
//                url: '{:url("train/today_timetable_forbidden")}', // 这么屌吗？tp框架能飞起来把js解析一次？
                url: '/admin/train/today_timetable_forbidden',
                method: "post",
                data: {
                    'id': data.id,
                    'status': status
                },
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    });
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 0) {
                        setTimeout(function () {
                            tableIns.reload();
                        }, 1000);
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
        }
    });
});